using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Client;
using FreeFlow.Client.Events;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// Control to display a folder at a stage.
	/// </summary>
  public class FolderPageControl : ConnectableControl
  {
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.Button closeButton;
    private System.Windows.Forms.TabControl tabControl;
    private System.Windows.Forms.Panel buttonPanel;

    private FolderPage folderPage;
    /// <summary>
    /// Creates a new <see cref="FolderPageControl"/> instance.
    /// </summary>
    public FolderPageControl()
    {
      // This call is required by the Windows.Forms Form Designer.
      InitializeComponent();

      folderPage = new FolderPage();
    }

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Component Designer generated code
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.buttonPanel = new System.Windows.Forms.Panel();
      this.closeButton = new System.Windows.Forms.Button();
      this.panel2 = new System.Windows.Forms.Panel();
      this.tabControl = new System.Windows.Forms.TabControl();
      this.buttonPanel.SuspendLayout();
      this.panel2.SuspendLayout();
      this.SuspendLayout();
      // 
      // buttonPanel
      // 
      this.buttonPanel.Controls.Add(this.closeButton);
      this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
      this.buttonPanel.Location = new System.Drawing.Point(0, 248);
      this.buttonPanel.Name = "buttonPanel";
      this.buttonPanel.Size = new System.Drawing.Size(616, 40);
      this.buttonPanel.TabIndex = 0;
      // 
      // closeButton
      // 
      this.closeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.closeButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.closeButton.Location = new System.Drawing.Point(536, 8);
      this.closeButton.Name = "closeButton";
      this.closeButton.TabIndex = 0;
      this.closeButton.Text = "Close";
      this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
      // 
      // panel2
      // 
      this.panel2.Controls.Add(this.tabControl);
      this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
      this.panel2.Location = new System.Drawing.Point(0, 0);
      this.panel2.Name = "panel2";
      this.panel2.Size = new System.Drawing.Size(616, 248);
      this.panel2.TabIndex = 1;
      // 
      // tabControl
      // 
      this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tabControl.Location = new System.Drawing.Point(0, 0);
      this.tabControl.Name = "tabControl";
      this.tabControl.SelectedIndex = 0;
      this.tabControl.Size = new System.Drawing.Size(616, 248);
      this.tabControl.TabIndex = 0;
      this.tabControl.SelectedIndexChanged += new System.EventHandler(this.tabControl_SelectedIndexChanged);
      // 
      // FolderPageControl
      // 
      this.Controls.Add(this.panel2);
      this.Controls.Add(this.buttonPanel);
      this.Name = "FolderPageControl";
      this.Size = new System.Drawing.Size(616, 288);
      this.buttonPanel.ResumeLayout(false);
      this.panel2.ResumeLayout(false);
      this.ResumeLayout(false);

    }
    #endregion

    #region properties
    /// <summary>
    /// Gets or sets the folder id.
    /// </summary>
    public string FolderId
    {
      get
      {
        return folderPage.FolderId;
      }
      set
      {
        if (Connection != null)
        {
          folderPage.FolderId = value;
          Populate();
        }
      }
    }

    /// <summary>
    /// Gets the preferred height of the control.
    /// </summary>
    public int PreferredHeight
    {
      get
      {
        if (folderPage.FormCount > 0)
          return folderPage.Form(0).Height + buttonPanel.Height;
        
        return Height;
      }
    }

    /// <summary>
    /// Gets the preferred size of the control.
    /// </summary>
    public Size PreferredControlSize
    {
      get
      {
        return new Size(PreferredWidth, PreferredHeight);
      }
    }

    /// <summary>
    /// Gets the preferred width of the control.
    /// </summary>
    public int PreferredWidth
    {
      get
      {
        if (folderPage.FormCount > 0)
          return folderPage.Form(0).Width;

        return Width; 
      }
    }

    /// <summary>
    /// Gets the name of the folder.
    /// </summary>
    public string FolderName
    {
      get
      {
        return folderPage.FolderName;
      }
    }
    
    /// <summary>
    /// Gets or sets the connection.
    /// </summary>
    public override Connection Connection
    {
      get
      {
        return base.Connection;
      }
      set
      {
        base.Connection = value;
        folderPage.Connection = value;
      }
    }

    #endregion

    private void Populate()
    {
      CreateTabs();
      CreateActionButtons();
      PopulateForm();
    }

    private void CreateTabs()
    {
      tabControl.TabPages.Clear();
      for (int i = 0; i < folderPage.FormCount; i++)
      {
        TabPage page = new TabPage();
        page.Text = folderPage.FormName(i);
        tabControl.TabPages.Add(page);

        FormControl form = new FormControl();
        form.Dock = DockStyle.Fill;
        page.Controls.Add(form);
      }
    }

    private void CreateActionButtons()
    {
      const int spacing = 5;
      int currentPos = spacing;
      for (int i = 0; i < folderPage.ActionCount; i++)
      {
        Button button = new Button();
        button.FlatStyle = FlatStyle.System;
        button.Text = folderPage.Action(i).Name;
        button.Top = closeButton.Top;
        button.Left = currentPos;
        currentPos = currentPos + button.Width + spacing;
        button.Click += new EventHandler(button_Click);
        buttonPanel.Controls.Add(button);
      }
    }

    /// <summary>
    /// Event triggered when an action button is pressed. This event will not be triggered for confirm 
    /// and non-confirm actions because these are handled by the control.
    /// </summary>
    [Category(Constants.FreeFlowCategory)]
    public event EventHandler<ActionButtonClickEventArgs> ActionButtonClick; 

    private void button_Click(object sender, EventArgs e)
    {
      string actionName = (sender as Button).Text;

      Action action = folderPage.Action(actionName);
      if (action != null)
      {
        if (action.Type == UserActionType.Form)
        {
          if (ActionButtonClick != null)
            ActionButtonClick(this, new ActionButtonClickEventArgs(actionName));
        }
        // handle confirm and non-confirm actions ourselves
        else if ((action.Type == UserActionType.NonConfirm) || (action.Type == UserActionType.Confirm))
        {
          ActionPage actionPage = new ActionPage();
          actionPage.Connection = Connection;

          if (action.Type == UserActionType.Confirm)
          {
            if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
              actionPage.StartAction(FolderId, action.Name);
              actionPage.Submit();
              OnCloseFolder();
            }
          }
          else
          {
            actionPage.StartAction(FolderId, action.Name);
            actionPage.Submit();
            OnCloseFolder();
          }
        }
      }
    }

    private void PopulateForm()
    {
      TabPage activePage = tabControl.SelectedTab;
      if (activePage != null)
      {
        FormControl formCtrl = activePage.Controls[0] as FormControl;
        if (formCtrl.Controls.Count == 0)
        {
          formCtrl.Populate(folderPage.Form(activePage.Text));
          formCtrl.MakeFieldsReadOnly();
        }
      }
    }

    private void closeButton_Click(object sender, System.EventArgs e)
    {
      OnCloseFolder();
    }

    private void OnCloseFolder()
    {
      if (CloseFolder != null)
        CloseFolder(this, EventArgs.Empty);
    }

    private void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      PopulateForm();
    }

    /// <summary>
    /// Event triggered when the folder has been closed.
    /// </summary>
    [Category(Constants.FreeFlowCategory)]
    public event EventHandler CloseFolder;
	}
}
